﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace _1.OrderService
{
    public partial class AddOrderForm : Form
    {
        Form1 _form1;
        OrderService _orderService;
        public AddOrderForm(Form1 form1,OrderService orderService)
        {
            InitializeComponent();
            _form1 = form1;
            _orderService = orderService;
        }

        private void AddOrderForm_Load(object sender, EventArgs e)
        {

        }

        private void button_quit_Click(object sender, EventArgs e)
        {
            _form1.Show();
            this.Close();
        }

        private bool IsNumber(string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;
            const string pattern = "^[0-9]*$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }

        //判断是不是实数
        private bool IsAllNumber(string text)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
            return !objNotNumberPattern.IsMatch(text) && !objTwoDotPattern.IsMatch(text) && !objTwoMinusPattern.IsMatch(text) && objNumberPattern.IsMatch(text);
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            string orderId = textBox_id.Text;
            if(!IsNumber(orderId))
            {
                DialogResult dr = MessageBox.Show("订单单号输入不合法请重新输入", "警告", MessageBoxButtons.OK);
                textBox_id.Text = "";
                return;
            }
            long orderID = Convert.ToInt64(orderId);
            string productName = textBox_product.Text;
            string customName = textBox_custom.Text;
            string money = textBox_money.Text;
            if(!IsAllNumber(money))
            {
                DialogResult dr = MessageBox.Show("金额输入不合法请重新输入", "警告", MessageBoxButtons.OK);
                textBox_money.Text = "";
                return;
            }
            double moneyd = double.Parse(money);
            Order newOrder = new Order(orderID,productName,customName, moneyd);
            if(_orderService.addOrder(newOrder))
            {
                DialogResult dr = MessageBox.Show("订单添加成功", "提示", MessageBoxButtons.OK);                
            }
            else
            {
                DialogResult dr = MessageBox.Show("订单已存在，添加失败", "提示", MessageBoxButtons.OK);
            }
            textBox_id.Text = "";
            textBox_custom.Text = "";
            textBox_money.Text = "";
            textBox_product.Text = "";
        }
    }
}
